﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileDB;

namespace Rem.Objects.Forms
{
    public partial class QueryForm : BaseForm
    {

        public QueryForm()
            : this(BaseObjectTypes.Queries, "查询")
        {

        }

        protected override void OnClosed(EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            base.OnClosed(e);
        }

        public static void OpenContractQuery(int type)
        {
            string text = "提醒查询";
            object datasource = null;
            QueryContract(type, ref text, ref datasource);

            QueryForm f = new QueryForm(BaseObjectTypes.ContractQuery, text, datasource);
            f.QueryID = type;
            f.Show();
            f.Dock = DockStyle.Fill;

        }

        private static void QueryContract(int type, ref string text, ref object datasource)
        {
            text = "合同查询";
            datasource = null;
            if (type == 1)
            {
                text = "本月合同";
                datasource = Contract.ThisMonthContracts();
            }
            else if (type == 2)
            {
                text = "上月合同";
                datasource = Contract.LastMonthContracts();
            }
            else if (type == 3)
            {
                text = "本年合同";
                datasource = Contract.ThisYearContracts();
            }
            else if (type == 10)
            {
                text = "所有合同";
                datasource = Contract.GetAllContract();
            }
        }

        public static void OpenReminderQuery(int type)
        {
            string text = "提醒查询";
            object datasource = null;
            QueryReminders(type, ref text, ref datasource);
            QueryForm f = new QueryForm(BaseObjectTypes.ReminderQuery, text, datasource);
            f.QueryID = type;
            f.Dock = DockStyle.Fill;
            f.Show();
        }

        private static void QueryReminders(int type, ref string text, ref object datasource)
        {

            if (type == 1)
            {
                text = "当日提醒";
                datasource = Reminder.CurrentReminders();
            }

            else if (type == 2)
            {
                text = "本月提醒";
                datasource = Reminder.ThisMonthReminders();
            }
            else if (type == 3)
            {
                text = "未处理提醒";
                datasource = Reminder.NotFinishedReminders();
            }
            else if (type == 4)
            {
                text = "交租提醒";
                datasource = Reminder.PostRentReminders();
            }
            else if (type == 5)
            {
                text = "收租提醒";
                datasource = Reminder.GetRentReminders();
            }
            else if (type == 6)
            {
                text = "合同提醒";
                datasource = Reminder.ContractReminders();
            }
            else if (type == 7)
            {
                text = "重要提醒";
                datasource = Reminder.GetImportantReminders();
            }
            else if (type == 8)
            {
                text = "续约提醒";
                datasource = Reminder.GetRenewReminders();
            }
            else if (type == 9)
            {
                text = "所有提醒";
                datasource = (from r in Reminder.GetAllReminder()
                    orderby r.ReminderTime ascending
                    select r).ToList();
            }
        }

        public static void OpenReminderQuery(int type, string title, object datasource)
        {
            QueryForm f = new QueryForm(BaseObjectTypes.ReminderQuery, title, datasource);
            f.QueryID = type;
            f.Dock = DockStyle.Fill;
            f.Show();
        }

        public QueryForm(BaseObjectTypes baseType, string text, Object datasource)
            : base(baseType, text)
        {
            InitializeComponent();

            Initialization(baseType, text, datasource);
        }

        public int QueryID { get; set; }
        private void Initialization(BaseObjectTypes baseType, string text, Object datasource)
        {
            this.BaseType = baseType;
            this.Text = text;
            Object dataSource = datasource;
            Dictionary<string, string> fieldNameHeadTextMapping = null;
            List<string> invisiableFieldNames = null;

            switch (baseType)
            {
                case BaseObjectTypes.Employee:
                    break;
                case BaseObjectTypes.EmployeeQuery:
                    if (datasource == null)
                        dataSource = RemDatabaseEntities.Employees;
                    fieldNameHeadTextMapping = QueryConstants.EmployeeFieldNameHeadTextMapping;
                    invisiableFieldNames = QueryConstants.EmployeeInvisiableFieldNames;
                    break;
                case BaseObjectTypes.Contract:

                    break;
                case BaseObjectTypes.ContractQuery:
                    if (datasource == null)
                        dataSource = RemDatabaseEntities.Contracts;
                    fieldNameHeadTextMapping = QueryConstants.ContractFieldNameHeadTextMapping;
                    invisiableFieldNames = QueryConstants.ContractInvisiableFieldNames;
                    break;
                case BaseObjectTypes.Customer:
                    break;
                case BaseObjectTypes.CustomerQuery:
                    if (datasource == null)
                        dataSource = RemDatabaseEntities.Customers;
                    fieldNameHeadTextMapping = QueryConstants.CustomerFieldNameHeadTextMapping;
                    invisiableFieldNames = QueryConstants.CustomerInvisiableFieldNames;
                    break;
                case BaseObjectTypes.Reminder:
                    break;
                case BaseObjectTypes.ReminderQuery:
                    if (datasource == null)
                        dataSource = RemDatabaseEntities.Reminders;
                    fieldNameHeadTextMapping = QueryConstants.ReminderFieldNameHeadTextMapping;
                    invisiableFieldNames = QueryConstants.ReminderInvisiableFieldNames;
                    break;
                default:
                    break;
            }
            this.ucDataGrid1.ObjectType = baseType;
            this.ucDataGrid1.InitializateGridView(dataSource, fieldNameHeadTextMapping, invisiableFieldNames);
        }

        public Object Data { set { this.ucDataGrid1.Data = value; } get { return this.ucDataGrid1.Data; } }
        public QueryForm(BaseObjectTypes baseType, string text)
            : this(baseType, text, null)
        {
        }

        private object GetDataSource(BaseObjectTypes basetype, int queryid)
        {
            object datasource = null;
            string text = null;
            switch (basetype)
            {
                case BaseObjectTypes.ContractQuery:
                    QueryContract(queryid, ref text, ref datasource);
                    break;
                case BaseObjectTypes.ReminderQuery:
                    QueryReminders(queryid, ref text, ref datasource);
                    break;
                case BaseObjectTypes.CustomerQuery:
                    QueryCustomers(queryid, ref text, ref datasource);
                    break;
                case BaseObjectTypes.EmployeeQuery:
                    QueryEmployees(queryid, ref text, ref datasource);
                    break;
                default:
                    break;
            }
            return datasource;
        }

       
        private void QueryEmployees(int queryid, ref string text, ref object datasource)
        {

            switch (queryid)
            {
                case 1:
                    text = "所有员工";
                    datasource = RemDatabaseEntities.Employees;
                    break;
                default:
                    break;
            }
        }


        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            if (this.QueryID > 0)
            {
                this.Cursor = Cursors.Arrow;
                try
                {
                    Initialization(this.BaseType, this.Text, GetDataSource(this.BaseType, this.QueryID));
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除吗？", "数据删除", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
            {
                return;
            }
            try
            {
                this.Cursor = Cursors.Arrow;

                if (this.QueryID > 0 && this.ucDataGrid1.DeleteSelected())
                {
                    Initialization(this.BaseType, this.Text, GetDataSource(this.BaseType, this.QueryID));
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }


        public static void OpenCustomerQuery(int type)
        {
            string text = "客户查询";
            object datasource = null;
            QueryCustomers(type, ref text, ref datasource);
            QueryForm f = new QueryForm(BaseObjectTypes.CustomerQuery, text, datasource);
            f.QueryID = type;
            f.Dock = DockStyle.Fill;
            f.Show();
        }

        private static void QueryCustomers(int type, ref string text, ref object datasource)
        {
            switch (type)
            {
                case 1:
                    text = "所有客户";
                    datasource = RemDatabaseEntities.Customers;
                    break;
                default:
                    break;
            }
        }

        private void QueryForm_Activated(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void ucDataGrid1_Load(object sender, EventArgs e)
        {

        }
    }
}
